package edu.nf.ch06.config;

import edu.nf.ch06.filter.Myfilter;
import edu.nf.ch06.filter.TestsFilter;
import org.springframework.boot.web.servlet.FilterRegistration;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author XCH
 * @date 2025 - 09 - 12
 */
@Configuration
/**@ServletComponentScan注解
 *是spring boot提供的专门用于扫描servlet组件（Servlet、Filter、Listener）
 * 注意：这种扫描有缺点，当多个过滤器形成过虑链的时候，会按照顺序执行，
 */
//@ServletComponentScan(basePackages = "edu.nf.ch06.filter")
public class McvConfig {
    /**
     * 第二种方式使用springboot提供的动态注册器（RegistrationBean）
     * 来注册Servlet组件，好处是可以控制过滤链的执行顺序
     * 有三种动态注册器分别注册Servlet、Filter、Listener
     * ServletRegistrationBean
     * FilterRegistrationBean
     * ServletListenerRegistrationBean
     */

    @Bean
    public FilterRegistrationBean TestFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new TestsFilter());
        registration.setName("TestsFilter");
        registration.addUrlPatterns("/*");
        registration.setOrder(1);
        return registration;
    }
    @Bean
    public FilterRegistrationBean MyFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new Myfilter());
        registration.setName("MyFilter");
        registration.addUrlPatterns("/*");
        //设置优先级别，数值越小优先级越高
        registration.setOrder(2);
        return registration;
    }
}
